package com.tjbklx1.redis.me;

import java.util.HashSet;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

public class RedisString {

	/**
	 * 获取数据
	 * 
	 * @param key
	 * @return
	 */
	public static String get(String key) {
		String value = null;

		JedisPool pool = null;
		Jedis jedis = null;
		try {
			pool = RedisConnection.getPool();
			jedis = pool.getResource();
			value = jedis.get(key);
		} catch (Exception e) {
			// 释放redis对象
			// pool.returnBrokenResource(jedis);
			jedis.close();
			e.printStackTrace();
		} finally {
			// 返还到连接池
			RedisConnection.returnResource(pool, jedis);
		}

		return value;
	}

	/**
	 * 存储数据
	 * 
	 * @param key
	 * @return
	 */
	public static void set(String key, String value) {
		if (StringUtils.isEmpty(value) || StringUtils.isEmpty(value)) {
			return;
		}
		JedisPool pool = null;
		Jedis jedis = null;
		try {
			pool = RedisConnection.getPool();
			jedis = pool.getResource();
			jedis.set(key, value);

		} catch (Exception e) {
			// 释放redis对象
			pool.returnBrokenResource(jedis);
			e.printStackTrace();
		} finally {
			// 返还到连接池
			RedisConnection.returnResource(pool, jedis);
		}
	}

	/**
	 * 描述:元素递增
	 * 
	 * @param key
	 * @author shiqingshan
	 * @date 20160420
	 * 
	 */
	public static void incr(String key) {
		if (StringUtils.isEmpty(key)) {
			return;
		}
		JedisPool pool = null;
		Jedis jedis = null;
		try {
			pool = RedisConnection.getPool();
			jedis = pool.getResource();
			jedis.incr(key);
		} catch (Exception e) {
			// 释放redis对象
			pool.returnBrokenResource(jedis);
			e.printStackTrace();
		} finally {
			// 返还到连接池
			RedisConnection.returnResource(pool, jedis);
		}
	}

	/**
	 * 保存key，并且设置过期时间
	 * 
	 * @param key
	 * @param time
	 *            秒
	 * @param value
	 */
	public static void setex(String key, int time, String value) {
		if (StringUtils.isEmpty(value) || StringUtils.isEmpty(value)) {
			return;
		}

		JedisPool pool = null;
		Jedis jedis = null;
		try {
			pool = RedisConnection.getPool();
			jedis = pool.getResource();
			jedis.setex(key, time, value);
			// jedis.expire(key, time);
		} catch (Exception e) {
			// 释放redis对象
			pool.returnBrokenResource(jedis);
			e.printStackTrace();
		} finally {
			// 返还到连接池
			RedisConnection.returnResource(pool, jedis);
		}
	}
	
	/**
	 * 删除一个key
	 * 
	 * @param key
	 * @param value
	 */
	public static Long del(String key) {
		Long ret = 0L;
		if (StringUtils.isEmpty(key)) {
			return ret;
		}

		JedisPool pool = null;
		Jedis jedis = null;
		try {
			pool = RedisConnection.getPool();
			jedis = pool.getResource();
			ret = jedis.del(key);
		} catch (Exception e) {
			// 释放redis对象
			pool.returnBrokenResource(jedis);
			e.printStackTrace();
		} finally {
			// 返还到连接池
			RedisConnection.returnResource(pool, jedis);
		}
		return ret;
	}
	
	/**
	 * 获取keys
	 * 
	 * @author Arno
	 * @param pattern
	 * @return
	 */
	public static Set<String> getKeys(String pattern) {
		Set<String> keys = new HashSet<String>();
		JedisPool pool = null;
		Jedis jedis = null;
		try {
			pool = RedisConnection.getPool();
			jedis = pool.getResource();
			keys = jedis.keys(pattern);
		} catch (Exception e) {
			// 释放redis对象
			pool.returnBrokenResource(jedis);
			e.printStackTrace();
		} finally {
			// 返还到连接池
			RedisConnection.returnResource(pool, jedis);
		}
		return keys;
	}

	/**
	 * 删除keys
	 * 
	 * @author Arno
	 * @param pattern
	 * @return
	 */
	public static long delKeys(String... keys) {
		JedisPool pool = null;
		Jedis jedis = null;
		long i = 0L;
		try {
			pool = RedisConnection.getPool();
			jedis = pool.getResource();
			i = jedis.del(keys);
		} catch (Exception e) {
			// 释放redis对象
			pool.returnBrokenResource(jedis);
			e.printStackTrace();
		} finally {
			// 返还到连接池
			RedisConnection.returnResource(pool, jedis);
		}
		return i;
	}

	/**
	 * 获取key
	 * 
	 * @author Arno
	 * @param pattern
	 * @return
	 */
	public static String getsetKey(String key, String value) {
		if (StringUtils.isEmpty(value) || StringUtils.isEmpty(value)) {
			return null;
		}

		JedisPool pool = null;
		Jedis jedis = null;
		try {
			pool = RedisConnection.getPool();
			jedis = pool.getResource();
			String oldValue = jedis.getSet(key, value);
			return oldValue;
		} catch (Exception e) {
			// 释放redis对象
			pool.returnBrokenResource(jedis);
			e.printStackTrace();
		} finally {
			// 返还到连接池
			RedisConnection.returnResource(pool, jedis);
		}
		return null;
	}

	/**
	 * 针对key增加指定value
	 * 
	 * @param key
	 * @param value
	 * @return key+value
	 * @author shiqingshan
	 */
	public static Long incrby(String key, Long value) {
		JedisPool pool = null;
		Jedis jedis = null;
		try {
			pool = RedisConnection.getPool();
			jedis = pool.getResource();
			Long newValue = jedis.incrBy(key, value);
			return newValue;
		} catch (Exception e) {
			// 释放redis对象
			pool.returnBrokenResource(jedis);
			e.printStackTrace();
		} finally {
			// 返还到连接池
			RedisConnection.returnResource(pool, jedis);
		}
		return null;

	}

	/**
	 * 针对key减去指定value
	 * 
	 * @param key
	 * @param value
	 * @return key-value
	 * @author shiqingshan
	 */
	public static Long decrby(String key, Long value) {

		JedisPool pool = null;
		Jedis jedis = null;
		try {
			pool = RedisConnection.getPool();
			jedis = pool.getResource();
			Long newValue = jedis.decrBy(key, value);
			return newValue;
		} catch (Exception e) {
			// 释放redis对象
			pool.returnBrokenResource(jedis);
			e.printStackTrace();
		} finally {
			// 返还到连接池
			RedisConnection.returnResource(pool, jedis);
		}
		return null;
	}
}
